/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditSupplier.java
 *
 * Created on Nov 5, 2011, 4:25:11 PM
 */
package mpinventory;

import java.sql.*;
import java.util.List;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author msi
 */
public class EditSupplier extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    public SupplierQuery supplierQuery;
    private List<Supplier> suppliers;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    /** Creates new form EditUser */
    public EditSupplier(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        initializeSupplierList();
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSESave = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtSEConNum = new javax.swing.JTextField();
        txtSEAddress = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtSEName = new javax.swing.JTextField();
        txtSESuppID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnSECancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        btnSESave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnSESave.setText("Save");
        btnSESave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("<html>Contact<br> Number/s:</html>");

        txtSEConNum.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        txtSEAddress.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        txtSEAddress.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Address:");

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Name:");

        txtSEName.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        txtSESuppID.setEditable(false);
        txtSESuppID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Supplier ID:");

        btnSECancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnSECancel.setText("Cancel");
        btnSECancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Edit Supplier Information");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSEName, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(txtSEAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(txtSEConNum, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(txtSESuppID, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(btnSESave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(59, 59, 59)
                        .addComponent(btnSECancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(57, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSESuppID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtSEName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSEAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSEConNum, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSECancel)
                    .addComponent(btnSESave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initializeSupplierList(){
        supplierQuery=new SupplierQuery(conn);
        suppliers=supplierQuery.initializeSuppliers();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<suppliers.size();i++)
            listModel.addElement(suppliers.get(i).getName());
        listSupplier.setModel(listModel);
        int j=listSupplier.getSelectedIndex()+1; //+1 because index of lists starts at -1
        txtSEName.setText(suppliers.get(j).getName());
        txtSESuppID.setText(suppliers.get(j).getID()+"");
        txtSEAddress.setText(suppliers.get(j).getAddress()+"");
        txtSEConNum.setText(suppliers.get(j).getContactNumbers()+"");
        listSupplier.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    int j=listSupplier.getSelectedIndex();
                    suppliers=supplierQuery.initializeSuppliers();
                    txtSEName.setText(suppliers.get(j).getName());
                    txtSESuppID.setText(suppliers.get(j).getID()+"");
                    txtSEAddress.setText(suppliers.get(j).getAddress()+"");
                    txtSEConNum.setText(suppliers.get(j).getContactNumbers()+"");
                }catch(Exception f){}
            }
        });
    }
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
        Object[] options=new String[]{"Yes","No"};
        pane.setOptions(options);
        JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
        dialog.show();
        if(options[0].equals(pane.getValue())) //YES
            backToMainMenu=true;
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        supplierQuery=new SupplierQuery(conn);
        if(txtSEName.getText().equals("") || txtSEAddress.getText().equals("") || txtSEConNum.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to edit this supplier?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Edition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                supplierQuery.editSupplier(Integer.parseInt(txtSESuppID.getText()),txtSEName.getText(),txtSEAddress.getText(),txtSEConNum.getText(),listSupplier.getSelectedValue().toString());
                int j=listSupplier.getSelectedIndex();
                DefaultListModel listModel = new DefaultListModel();
                suppliers=supplierQuery.initializeSuppliers();
                for(int i=0;i<suppliers.size();i++)
                    listModel.addElement(suppliers.get(i).getName());
                listSupplier.setModel(listModel);
                listSupplier.setSelectedIndex(j); //update supplier list
            } //YES
        }
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSECancel;
    private javax.swing.JButton btnSESave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtSEAddress;
    private javax.swing.JTextField txtSEConNum;
    private javax.swing.JTextField txtSEName;
    private javax.swing.JTextField txtSESuppID;
    // End of variables declaration//GEN-END:variables
}
